from django.core.management.base import BaseCommand
from django.contrib.auth.hashers import make_password
from apps.users.models import User
from datetime import datetime

class Command(BaseCommand):
    help = '创建测试用户'

    def handle(self, *args, **options):
        # 创建测试用户列表
        test_users = [
            {
                'username': 'itcast_02',
                'mobile': '18761230001',
                'email': 'itcast02@example.com',
                'password': '123456',
                'is_superuser': True,
                'is_staff': True,
                'is_active': True,
                'email_active': True
            },
            {
                'username': 'itcast_03',
                'mobile': '18761230002',
                'email': 'itcast03@example.com',
                'password': '123456',
                'is_superuser': False,
                'is_staff': True,
                'is_active': True,
                'email_active': True
            },
            {
                'username': 'itcast_04',
                'mobile': '18761230003',
                'email': 'itcast04@example.com',
                'password': '123456',
                'is_superuser': False,
                'is_staff': False,
                'is_active': True,
                'email_active': True
            },
            {
                'username': 'itcast_05',
                'mobile': '18761230004',
                'email': 'itcast05@example.com',
                'password': '123456',
                'is_superuser': False,
                'is_staff': False,
                'is_active': True,
                'email_active': False
            },
            {
                'username': 'itcast_06',
                'mobile': '18761230005',
                'email': 'itcast06@example.com',
                'password': '123456',
                'is_superuser': False,
                'is_staff': True,
                'is_active': False,
                'email_active': True
            }
        ]

        # 创建用户
        for user_data in test_users:
            # 加密密码
            user_data['password'] = make_password(user_data['password'])

            # 设置日期时间
            user_data['date_joined'] = datetime.now()

            # 创建用户
            user = User.objects.create(**user_data)

            self.stdout.write(
                self.style.SUCCESS(f'成功创建用户: {user.username} (is_superuser={user.is_superuser}, is_staff={user.is_staff}, is_active={user.is_active})')
            )

        self.stdout.write(self.style.SUCCESS('所有测试用户创建完成!'))
